// 编写一个程序，找到两个单链表相交的起始节点。

const Node = require('../../单链表/node');


var getIntersectionNode = function (headA, headB) {
    function size(head) {
        let count = 0
        let p = head
        while (p !== null) {
            count++
            p = p.next
        }
        return count
    }
    let delta = size(headA) - size(headB)
    let pa = headA
    let pb = headB
    if (delta >= 0) {
        // 表A长
        for (let i = 0; i < delta; i++) {
            pa = pa.next
        }
    } else {
        //表B长
        for (let i = 0; i < Math.abs(delta); i++) {
            pb = pb.next
        }
    }
    while (pa !== pb && pa !== null) {
        pa = pa.next
        pb = pb.next
    }
    return pa !== null ? pa : null
}

let head = new Node(4)
head.next = new Node(1)
head.next.next = new Node(8)
head.next.next.next = new Node(4)
head.next.next.next.next = new Node(5)

let head0 = new Node(5)
head0.next = new Node(0)
head0.next.next = new Node(1)
head0.next.next.next = head.next.next


console.log(getIntersectionNode(head,head0));